//
//  MainNaviViewController.m
//  MyMicroBlog
//
//  Created by Apple on 16/9/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "MainNaviViewController.h"
#import "UIBarButtonItem+Extension.h"
@interface MainNaviViewController ()<UIGestureRecognizerDelegate>

@end

@implementation MainNaviViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
}
//处理边缘手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    //如果是根视图不处理
    if (self.viewControllers.count == 1) {
        return NO;
    }
    return YES;
}

//重写push方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //子控制器个数大于0,说明不是根控制器
    if (self.childViewControllers.count > 0) {
        //当控制器为第二级时
        if (self.childViewControllers.count == 1) {
            //获取上一级的title
            NSString *title = self.childViewControllers.firstObject.title;
            viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:title andImageName:nil andTarget:self andAction:@selector(backAction)];
        }else{
            viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"返回" andImageName:nil andTarget:self andAction:@selector(backAction)];
        }
        viewController.title = [NSString stringWithFormat:@"当前显示第%zd级控制器",self.viewControllers.count + 1];
    }
    [super pushViewController:viewController animated:YES];
}

//返回事件
-(void)backAction{
    [self popViewControllerAnimated:YES];
}

@end
